package com.ruoyi.business.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.business.domain.OrderManage;
import com.ruoyi.business.mapper.OrderManageMapper;
import com.ruoyi.business.service.IRecordFormingOrderManageService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Author Jensen
 * @see
 * @since 2022/8/22
 */
@Service
public class RecordFormingOrderManageServiceImpl implements IRecordFormingOrderManageService {
    @Autowired
    private OrderManageMapper orderManageMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    private Map<String, String> getUsers() {
        List<SysUser> users = sysUserMapper.selectUserList(new SysUser());
        Map<String, String> map = new HashMap<>();
        for (SysUser one : users) {
            map.put(one.getUserName(), one.getNickName());
        }
        return map;
    }

    @Override
    public List<List<Map<String, Object>>> getOrderReceiveNumbers(String startTime, String endTime) {
        List<OrderManage> orderManages = orderManageMapper.selectOrderReceiveNum(startTime, endTime);
        Map<String, String> map = getUsers();
        List<List<Map<String, Object>>> list = new ArrayList<>();
        //表头
        List<Map<String, Object>> head = new LinkedList<>();
        Map<String, Object> headMap = new HashMap<>();
        //body
        List<Map<String, Object>> body = new LinkedList<>();
        Map<String, Object> bodyMap = new LinkedHashMap<>();
        headMap.put("column_name", "姓名");
        head.add(headMap);
        bodyMap.put("姓名", "数量（个）");
        Long total = 0L;
        for (OrderManage one : orderManages) {
            if (map.get(one.getReceiveBy()) == null) {
                continue;
            }
            headMap = new HashMap<>();
            headMap.put("column_name", map.get(one.getReceiveBy()));
            head.add(headMap);
            bodyMap.put(map.get(one.getReceiveBy()), one.getId());
            total += one.getId();
        }
        headMap = new HashMap<>();
        headMap.put("column_name", "合计");
        head.add(headMap);
        bodyMap.put("合计", total);
        body.add(bodyMap);
        list.add(head);
        list.add(body);
        return list;
    }

    @Override
    public List<Map<String, Object>> getOrderSourceNumbers(String startTime, String endTime) {
        List<OrderManage> orderManages = orderManageMapper.selectOrderSourceNum(startTime, endTime);
        List<SysUser> users = sysUserMapper.selectUserList(new SysUser());
        Map<String, String> userMap = new HashMap<>();
        for (SysUser one : users) {
            userMap.put(one.getUserName(), one.getUserType());
        }
        List<Map<String, Object>> list = new ArrayList<>();
        //body
        Map<String, Object> bodyMap = new LinkedHashMap<>();
        bodyMap.put("source", "数量（个）");
        AtomicLong total = new AtomicLong(0);
        AtomicLong order1 = new AtomicLong(0);
        AtomicLong order2 = new AtomicLong(0);
        AtomicLong order3 = new AtomicLong(0);
        for (OrderManage one : orderManages) {
            if ("2".equals(userMap.get(one.getCreateBy()))) {
                order1.getAndAdd(one.getId());
                total.getAndAdd(one.getId());
            } else if ("3".equals(userMap.get(one.getCreateBy()))) {
                order2.getAndAdd(one.getId());
                total.getAndAdd(one.getId());
            }
        }
        bodyMap.put("ordinary", order1);
        bodyMap.put("personal", order2);
        bodyMap.put("total", total);
        list.add(bodyMap);
        return list;
    }

    @Override
    public List<Map<String, Object>> getOrderTypeNumbers(String startTime, String endTime) {
        List<OrderManage> orderManages = orderManageMapper.selectOrderTypeNum(startTime, endTime);
        List<Map<String, Object>> list = new ArrayList<>();
        //body
        Map<String, Object> bodyMap = new LinkedHashMap<>();
        bodyMap.put("orderType", "数量（个）");
        AtomicLong total = new AtomicLong(0);
        AtomicLong order1 = new AtomicLong(0);
        AtomicLong order2 = new AtomicLong(0);
        AtomicLong order3 = new AtomicLong(0);
        for (OrderManage one : orderManages) {
            if ("1".equals(one.getOrderType())) {
                order1.getAndAdd(one.getId());
            } else if ("2".equals(one.getOrderType())) {
                order2.getAndAdd(one.getId());
            } else if ("3".equals(one.getOrderType())) {
                order3.getAndAdd(one.getId());
            }
            total.getAndAdd(one.getId());
        }
        bodyMap.put("orderType1", order1);
        bodyMap.put("orderType2", order2);
        bodyMap.put("other", order3);
        bodyMap.put("orderType3", 0);
        bodyMap.put("orderType4", 0);
        bodyMap.put("orderType5", 0);
        bodyMap.put("orderTotal", total);
        list.add(bodyMap);
        return list;
    }

    @Override
    public List<Map<String, Object>> getOrderEvaluateNumbers(String startTime, String endTime) {
        List<OrderManage> orderManages = orderManageMapper.selectOrderEvaluateNum(startTime, endTime);
        List<Map<String, Object>> list = new ArrayList<>();
        //body
        Map<String, Object> bodyMap = new LinkedHashMap<>();
        bodyMap.put("evaluateResult", "数量（个）");
        AtomicLong total = new AtomicLong(0);
        AtomicLong order1 = new AtomicLong(0);
        AtomicLong order2 = new AtomicLong(0);
        AtomicLong order3 = new AtomicLong(0);
        AtomicLong order4 = new AtomicLong(0);
        AtomicLong order5 = new AtomicLong(0);
        for (OrderManage one : orderManages) {
            if ("5".equals(one.getEvaluateScore())) {
                order1.getAndAdd(one.getId());
            } else if ("4".equals(one.getEvaluateScore())) {
                order2.getAndAdd(one.getId());
            } else if ("3".equals(one.getEvaluateScore())) {
                order3.getAndAdd(one.getId());
            } else if ("2".equals(one.getEvaluateScore())) {
                order4.getAndAdd(one.getId());
            } else if ("1".equals(one.getEvaluateScore())) {
                order5.getAndAdd(one.getId());
            }
            total.getAndAdd(one.getId());
        }
        bodyMap.put("great", order1);
        bodyMap.put("good", order2);
        bodyMap.put("ordinary", order3);
        bodyMap.put("discontented", order4);
        bodyMap.put("veryDiscontented", order5);
        bodyMap.put("orderTotal", total);
        list.add(bodyMap);
        return list;
    }

    @Override
    public List<List<Map<String, Object>>> getOrderOperateTimelinessNumbers(String startTime, String endTime) {
        List<OrderManage> orderManages = orderManageMapper.selectOrderTimelinessNum(startTime, endTime);
        Map<String, String> users = getUsers();
        List<List<Map<String, Object>>> list = new ArrayList<>();
        //表头
        List<Map<String, Object>> head = new LinkedList<>();
        Map<String, Object> headMap = new HashMap<>();
        //body
        List<Map<String, Object>> body = new LinkedList<>();
        Map<String, Object> bodyMapOne = new LinkedHashMap<>();
        Map<String, Object> bodyMapTwo = new LinkedHashMap<>();
        Map<String, Object> bodyMapThree = new LinkedHashMap<>();
        Map<String, Object> bodyMapFour = new LinkedHashMap<>();
        headMap.put("column_name", "姓名");
        head.add(headMap);
        bodyMapOne.put("姓名", "工单数量（个）");
        bodyMapTwo.put("姓名", "最小时效（分钟）");
        bodyMapThree.put("姓名", "最大时效（分钟）");
        bodyMapFour.put("姓名", "平均时效（分钟）");
        Long total = 0L;
        Map<String, Map<String, Long>> map = new HashMap<>();
        Map<String, Long> childMap;
        Long minTime, maxTime, averageTime, temp;
        Date receiveTime, completeTime;
        for (OrderManage one : orderManages) {
            receiveTime = one.getReceiveTime();
            completeTime = one.getCompleteTime();
            temp = DateUtil.between(receiveTime, completeTime, DateUnit.MINUTE);
            if (map.get(one.getReceiveBy()) == null) {
                childMap = new HashMap<>();
                childMap.put("orderNum", 1L);
                childMap.put("minTime", temp);
                childMap.put("maxTime", temp);
                childMap.put("averageTime", temp);
            } else {
                childMap = map.get(one.getReceiveBy());
                minTime = childMap.get("minTime");
                maxTime = childMap.get("maxTime");
                averageTime = childMap.get("averageTime");
                if (minTime > temp) {
                    childMap.put("minTime", temp);
                }
                if (maxTime < temp) {
                    childMap.put("maxTime", temp);
                }
                childMap.put("averageTime", averageTime + temp);
                childMap.put("orderNum", childMap.get("orderNum") + 1L);
            }
            map.put(one.getReceiveBy(), childMap);
        }
        Iterator<String> iterator = map.keySet().iterator();
        String key;
        while (iterator.hasNext()) {
            key = iterator.next();
            childMap = map.get(key);
            key = users.get(key);
            if (key == null) {
                continue;
            }
            headMap = new HashMap<>();
            headMap.put("column_name", key);
            head.add(headMap);
            bodyMapOne.put(key, childMap.get("orderNum"));
            bodyMapTwo.put(key, childMap.get("minTime"));
            bodyMapThree.put(key, childMap.get("maxTime"));
            bodyMapFour.put(key, childMap.get("averageTime") / childMap.get("orderNum"));
        }
        body.add(bodyMapOne);
        body.add(bodyMapTwo);
        body.add(bodyMapThree);
        body.add(bodyMapFour);
        list.add(head);
        list.add(body);
        return list;
    }
}
